import { getItem, setItem } from '@/utils/storage'
import { LANG, TAGS_VIEW } from '@/constant'
import {privateRoutes } from "@//router"

export default {
  namespaced: true,
  state: () => ({
    routes: privateRoutes,
  }),
  mutations: {
    /**
     * 增加路由
     * @param state
     * @param newRoutes
     */
    setRoutes(state, newRoutes) {
      state.routes = [...privateRoutes, ...newRoutes]
    }
  },
  actions: {
    /**
     * 过滤路由
     * @param context
     * @param menus
     * @returns {Array}
     */
    filterRoutes({ commit }, menus) {
      const routes = []
      menus.forEach(route => {
        routes.push(...privateRoutes.filter(item => item.name === route))
      })
      // FIXME 页面不存在时不进入404
      // 最后添加 不匹配路由进入 404
      routes.push({
        path: '/:catchAll(.*)',
        redirect: '/404'
      })
      commit('setRoutes', routes)
      return routes
    }
  }
}
